const {
    override,
    addDecoratorsLegacy,
    addWebpackPlugin,
    addWebpackAlias,
    disableEsLint,
    fixBabelImports,
    overrideDevServer
} = require('customize-cra');
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const cesiumSource = 'node_modules/cesium/Source';
const cesiumWorkers = 'node_modules/cesium/Build/Cesium/Workers';
const replaceConfig = () => (config) => {
    fixBabelImports('import', {
        libraryName: 'antd',
        libraryDirectory: 'es',
        style: true,// # 这里不注释掉就无法使用less修改主题，这里的功能是样式按需加载
    });
    addDecoratorsLegacy();
    disableEsLint();//禁用eslint
    addWebpackAlias({cesium: path.resolve(__dirname, cesiumSource)});// Cesium模块名称
    addWebpackPlugin([
        // 拷贝Cesium 资源、控价、web worker到静态目录
        new CopyWebpackPlugin({
            patterns: [
                {from: path.join(cesiumSource, cesiumWorkers), to: 'Cesium/Workers'},
                {from: path.join(cesiumSource, 'Assets'), to: 'Cesium/Assets'},
                {from: path.join(cesiumSource, 'Widgets'), to: 'Cesium/Widgets'},
                {from: path.join(cesiumSource, 'Core'), to: 'Cesium/Core'}
            ]
        }),
        new webpack.DefinePlugin({
            //Cesium载入静态的资源的相对路径
            CESIUM_BASE_URL: JSON.stringify('')
        }),
    ]);
    config.output['sourcePrefix'] = ""; //需要编译Cesium中的多行字符串
    config['amd'] = {toUrlUndefined: true};//允许Cesium兼容 webpack的require方式
    // config['module']={unknownContextCritical:false} // 有问题地 //解决warning require的提示；require function is used in a way in which dependencies cannot be statically extracted
    config['node'] = {fs: 'empty'};// 解决fs模块的问题（Resolve node module use of fs）
    config['devtool'] = "eval";
    config.optimization.splitChunks = {
        name: true,     ////缓存组里面的filename生效，覆盖默认命名
        minSize: 20 * 1024, //字节 引入的文件大于2m才进行分割
        minChunks: 1     //模块至少使用次数
    };
    return config;

}
module.exports = {
    webpack: override(replaceConfig()),
    devServer: overrideDevServer()
}